﻿-- Author  : Kurapica
-- Create Date : 2010/03/21

do
	IGAS:NewAddon("IGAS_Studio")
	
	local _Index = 0
	local _AddonName	
	
	local frm = CreateFrame("Form", "ExportGuide", coreF)
	frm.Caption = L["Export Guide"]	
	frm.Width = 540
	frm.Height = 500
	frm.Visible = false
	
	local path = CreateFrame("SingleTextBox", "Path", frm)
	path:SetPoint("TOPLEFT", frm, "TOPLEFT", 4, -26)
	path:SetPoint("TOPRIGHT", frm, "TOPRIGHT", -4, -26)
	path.Height = 26
	
	local btnNext = CreateFrame("NormalButton", "Next", frm)
	btnNext:SetPoint("BOTTOMRIGHT", frm, "BOTTOMRIGHT", -4, 24)
	btnNext.Style = "NORMAL"
	btnNext.Height = 24
	btnNext.Width = 100
	btnNext.Text = L["Next"]
	
	local btnPrev = CreateFrame("NormalButton", "Prev", frm)
	btnPrev:SetPoint("RIGHT", btnNext, "LEFT", -4, 0)
	btnPrev.Style = "NORMAL"
	btnPrev.Height = 24
	btnPrev.Width = 100
	btnPrev.Text = L["Prev"]
	
	local content = CreateFrame("MultiLineTextBox", "Content", frm)
	content:SetPoint("TOPLEFT", path, "BOTTOMLEFT", 0, -6)
	content:SetPoint("BOTTOMRIGHT", btnNext, "TOPRIGHT", 0, 4)
	
	-- embeds file
	local _EmbedsFileName = [[
embeds.xml
]]
	local _EmbedsFileContent = [=[
<Ui xmlns="http://www.blizzard.com/wow/ui/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.blizzard.com/wow/ui/
..\FrameXML\UI.xsd">
    
	<Include file="IGAS\IGAS.xml"/>
</Ui>
]=]
	local _LastInfo = [=[
IMPORTANT: NOW COPY IGAS FOLDER TO YOUR ADDON FOLDER!!!:

FROM:
    World of Warcraft\Interface\Addons\IGAS_Studio\

TO:
    ]=]
	
	-- Scripts
	local files = {}
	
	local function LoadFile(file, path, toc)
		path = path..file.Name
		
		if file.Type == "Frame" then
			tinsert(files, {
				Path = path..".Designer.lua",
				Content = file.Designer,
			})
			tinsert(files, {
				Path = path..".Script.lua",
				Content = file.Script,
			})
			
			tinsert(toc, path..".Designer.lua")
			tinsert(toc, path..".Script.lua")
		elseif file.Content then
			tinsert(files, {
				Path = path..".lua",
				Content = file.Content,
			})
			
			tinsert(toc, path..".lua")
		end
	end
	
	local function LoadFolder(folder, path, toc)
		if getn(folder) == 0 then
			return
		end
		
		path = path..folder.Name.."\\"
		
		for _, file in ipairs(folder) do
			if type(file) == "table" then
				if file.Type == "Folder" then
					LoadFolder(file, path, toc)
				else
					LoadFile(file, path, toc)
				end
			end
		end
		
		tinsert(toc, "")
	end
	
	local function LoadAddon(name)	
		local addon = IGAS_STUDIO_DB.FileDB[name]
		
		local info = addon[0]		
		local toc = {}
		
		wipe(files)
		
		for _, v in ipairs(_TOC_FORMAT) do
			if v.Type == "String" and v.Name ~= "Name" then
				if info[v.Name] then
					tinsert(toc, "## "..v.Name..": "..tostring(info[v.Name]))
				end
			elseif v.Type == "Number" then
				if info[v.Name] then
					tinsert(toc, "## "..v.Name..": "..tostring(info[v.Name]))
				end
			elseif v.Type == "Set" then
				for _, loc in ipairs(_Local) do
					if info[v.Name..loc] then
						tinsert(toc, "## "..v.Name..loc..": "..tostring(info[v.Name..loc]))
					end
				end
			elseif v.Type == "Boolean" then
				tinsert(toc, "## "..v.Name..": "..info[v.Name])
			end
		end
		
		tinsert(toc, "")
		
		-- embeds xml file
		tinsert(toc, _EmbedsFileName)
		
		tinsert(files, {
			Path = "embeds.xml",
			Content = _EmbedsFileContent,
		})
		
		for _, f in ipairs(addon) do
			if type(f) == "table" then
				if f.Type == "Folder" then
					LoadFolder(f, "", toc)
				else
					LoadFile(f, "", toc)
					tinsert(toc, "")
				end
			end
		end
		
		tinsert(files, {
			Path = "The last thing to do!!!",
			Content = _LastInfo..[[World of Warcraft\Interface\Addons\]].._AddonName.."\\"
		})
		
		files[0] = {
			Path = ""..name..".toc",
			Content = table.concat(toc, "\n"),
		}
	end
	
	-- Custom Event
	frm:RegisterEvent("IGAS_STUDIO_EXPORT_ADDON")
	
	local function Refresh()
		btnPrev.Enabled = (_Index > 0)
		btnNext.Text = (_Index < getn(files) and L["Next"]) or L["Finish"]
		
		if _Index < getn(files) then
			path.Text = [[World of Warcraft\Interface\Addons\]].._AddonName.."\\"..files[_Index].Path
			content.Editable = true
		else
			path.Text = files[_Index].Path
			content.Editable = false
		end
		content.Text = files[_Index].Content
	end
	
	function frm:IGAS_STUDIO_EXPORT_ADDON(name)
		_AddonName = name		
		_Index = 0
		
		LoadAddon(name)		
		
		frm.Visible = true
		Refresh()
	end
	
	-- Script Handlers
	function btnPrev:OnClick()
		_Index = _Index - 1
		Refresh()
	end
	
	function btnNext:OnClick()
		_Index = _Index + 1
		if _Index > getn(files) then
			wipe(files)
			frm.Visible = false
		else
			Refresh()
		end
	end
end